package com.sdry.web.controller.lz;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.lz.DeviceAccount;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.model.lz.SpinningRecord;
import com.sdry.service.lz.ProductInspectService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;

/**
 * 
 * @ClassName ProductInspectController
 * @Description 生产抽检
 * @Author lz
 * @Date 2018年12月3日 10:07:47
 * @Version 1.0
 */
@Controller
@RequestMapping("/productInspect")
public class ProductInspectController {

	public static final Integer AFFECT_ROW = 0;
	@Resource
	ProductInspectService productInspectService;
	
	/**
     * 新增生产抽检信息
     * @param response
     * @param spinningRecord 生产抽检实体
     */
    @RequestMapping("/addProductInspect")
    public void addProductInspect(HttpServletResponse response, SpinningRecord spinningRecord) throws Exception {
    	if (spinningRecord != null) {
			if(StringUtil.isEmpty(spinningRecord.getNextRoundTime())){
				spinningRecord.setNextRoundTime(null);
			}
		}
        Long affectRow = productInspectService.addProductInspect(spinningRecord);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteProductInspectById")
    public void deleteProductInspectById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = productInspectService.deleteProductInspectById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改生产抽检
     * @param response
     * @param spinningRecord 选中行的信息
     * @throws Exception
     */
    @RequestMapping("/editProductInspectById")
    public void editDeviceAccountById(HttpServletResponse response, SpinningRecord spinningRecord) throws Exception {
    	if (spinningRecord != null) {
			if(StringUtil.isEmpty(spinningRecord.getNextRoundTime())){
				spinningRecord.setNextRoundTime(null);
			}
		}
        Long affectRow = productInspectService.editProductInspectById(spinningRecord);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询生产抽检信息
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryProductInspect")
    public void queryProductInspect(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<SpinningRecord> deviceList = productInspectService.queryProductInspect(criteria);
        int count = productInspectService.countProductInspect(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
